package io.geekidea.springbootplus.system.service;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import io.geekidea.springbootplus.system.utils.SmsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * @Classname SmsCodeService
 * @Description 发送短信验证码
 * @Date 2019/9/30 16:22
 * @Created by Yrd
 */
@Service
public class SmsCodeService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Value("${sms.template_code}")
    private String template_code;
    @Value("${sms.login_template}")
    private String login_template;
    @Value("${sms.register_template}")
    private String register_template;
    @Value("${sms.updatePhone_template}")
    private String updatePhone_template;

    @Value("${sms.sign_name}")
    private String sign_name;

//    @Resource
//    private MsgRecordMapper msgRecordMapper;

    public void createSmsCode(String phone) {
        new Thread(() -> {
            //生成6位随机数
            final String code = (long) (Math.random() * 1000000) + "";
            //存入缓存
            if (StringUtils.isEmpty(phone)){
                return;
            }
            redisTemplate.opsForValue().set(phone,code,800, TimeUnit.SECONDS);
            String redisCode = (String) redisTemplate.opsForValue().get(phone);
            JSONObject json = new JSONObject();
            json.put("code",code);
            String param = json.toString();
            try {

                SendSmsResponse response = SmsUtil.sendSms(phone, login_template, sign_name, param);
                SimpleDateFormat format = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");
                String format1 = format.format(new Date());
//                msgRecordMapper.addMsgRecord("短信", format1, phone);
            } catch (ClientException e) {
                e.printStackTrace();
            }
        }).start();



    }
    //不同的方式发送不同的模板消息
    public void createSmsCode(String phone,String method) {
        new Thread(() -> {
            //生成6位随机数
            final String code = (long) (Math.random() * 1000000) + "";
//            System.out.println(code);
            //存入缓存
            if (StringUtils.isEmpty(phone)){
                return;
            }
            redisTemplate.opsForValue().set(phone,code,120000, TimeUnit.SECONDS);
            String redisCode = (String) redisTemplate.opsForValue().get(phone);
            JSONObject json = new JSONObject();
            json.put("code",code);
            String param = json.toString();
            try {
                if (method.equals("login")){
                 SendSmsResponse response = SmsUtil.sendSms(phone, login_template, sign_name, param);
                }
                if (method.equals("register")){
                    SendSmsResponse response = SmsUtil.sendSms(phone, register_template, sign_name, param);
                }
                if (method.equals("updatePhone")){
                    SendSmsResponse response = SmsUtil.sendSms(phone, updatePhone_template, sign_name, param);
                }
                SimpleDateFormat format = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");
                String format1 = format.format(new Date());
//                msgRecordMapper.addMsgRecord("短信", format1, phone);
            } catch (ClientException e) {
                e.printStackTrace();
            }
        }).start();



    }
}

